package turistic.GUI.graphic;

//import turisztika.adatok.ablak.R;
//import turisztika.adatok.ablak.TurisztikaActivity.MyLocationListener;
//import turisztika.adatok.ablak.TurisztikaActivity;
//import turisztika.adatok.ablak.TurisztikaActivity.MyLocationListener;

//import com.javacodegeeks.android.lbs.LbsGeocodingActivity.MyLocationListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class ParamActivity extends Activity {
	protected LocationManager locationManager;
	ImageButton kordlekeres;
	EditText etLongitude;
	EditText etLatitude;
	private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
																		// Meters
	private static final long MINIMUM_TIME_BETWEEN_UPDATES = 100000; // in
																		// Milliseconds
	MyLocationListener myLL = new MyLocationListener();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parameters);

		Button next = (Button) findViewById(R.id.nextB);
		next.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				// startActivity(new Intent(ParamActivity.this,
				// RoutesActivity.class));
				EditText x = (EditText) findViewById(R.id.editText1);
				String xStr = x.getText().toString();
				EditText y = (EditText) findViewById(R.id.editText3);
				String yStr = y.getText().toString();
				// double xx = Double.parseDouble(xStr);
				// double yy = Double.parseDouble(yStr);
				// messageBox(xx + " " + yy);

				// double[] array = {xx,yy};
				if ((xStr.equals("")) || (yStr.equals(""))) {
					messageBox("Kerem adja meg a koordinatakat!");
				} else {
					Intent intent = new Intent(ParamActivity.this,
							RoutesActivity.class);
					intent.putExtra("Coordinates", xStr + " " + yStr);

					// final int result=1;
					startActivity(intent);
				}
			}
		});

		kordlekeres = (ImageButton) findViewById(R.id.refresh);
		etLongitude = (EditText) findViewById(R.id.editText1);
		etLatitude = (EditText) findViewById(R.id.editText3);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		kordlekeres.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				locationManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER,
						MINIMUM_TIME_BETWEEN_UPDATES,
						MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLL);
				showCurrentLocation();
				locationManager.removeUpdates(myLL);
				// locationManager = null;
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(ParamActivity.this,
					TurisztikaiUtvonalTervezoActivity.class));
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	protected void showCurrentLocation() {

		Location location = locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		if (location != null) {
			etLatitude.setText(location.getLongitude() + "");
			etLongitude.setText(location.getLatitude() + "");

		}

	}

	private class MyLocationListener implements LocationListener {

		public void onStatusChanged(String s, int i, Bundle b) {

		}

		public void onProviderDisabled(String s) {
			Toast.makeText(ParamActivity.this, "GPS kikapcsolva",
					Toast.LENGTH_LONG).show();
		}

		public void onProviderEnabled(String s) {
			Toast.makeText(ParamActivity.this, "GPS bekapcsolva",
					Toast.LENGTH_LONG).show();
		}

		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub

		}

	}

	private void messageBox(String mess) {
		final AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();

		alertDialog.setTitle("Uzenet");
		alertDialog.setMessage(mess);

		alertDialog.show();

	}

}
